package com.demo.websocketbarrage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

@Controller
public class GreetingController {



    @MessageMapping("/hello")
    @SendTo("/topic/greetings")//发送到
    public Greeting greeting(HelloMessage message) throws Exception {
        return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
    }

    @Autowired
    SimpMessagingTemplate msgTemp;

    @GetMapping("serverSendMsg")
    @ResponseBody
    public void serverSendMsg(){
        Greeting greeting = new Greeting("来自服务器的消息");
        msgTemp.convertAndSend("/topic/greetings",greeting);

    }


}
